package com.pxmeta.app.controller.business;


import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.product.entity.Product;
import com.pxmeta.product.entity.Shop;
import com.pxmeta.product.entity.UserShop;
import com.pxmeta.product.entity.dto.BuyDTO;
import com.pxmeta.product.entity.dto.NonuseDecorationDTO;
import com.pxmeta.product.entity.dto.UseDecorationDTO;
import com.pxmeta.product.service.IProductService;
import com.pxmeta.product.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(value = "商品模块",tags = "商品模块")
@RestController
@RequestMapping("/product")
@AllArgsConstructor
@CrossOrigin
public class ProductController {

    private final IProductService productService;

    private final IShopService shopService;

    @GetMapping("/getVirtualCurrency")
    @ApiOperation("获取虚拟币列表")
    public AjaxResult<List<Product>> getVirtualCurrencyList(@RequestParam(required = false) String platform){
        List<Product> list = productService.getVirtualCurrencyList();
//        if("ios".equals(platform)){
//            list = list.stream().filter(l->l.getAppleCount().doubleValue()).collect(Collectors.toList());
//            for (Product product : list) {
//                product.setCount(product.getAppleCount());
//            }
//        }

        return AjaxResult.success(list);
    }


    @GetMapping("/getShopIndexList")
    @ApiOperation("获取商城首页商品列表")
    public AjaxResult getShopIndexList(){
        Map<String, List<Shop>> result = shopService.list().stream().collect(Collectors.groupingBy(Shop::getCategoryName));
        return AjaxResult.success(result);
    }


    @PostMapping("/buyDecoration")
    @ApiOperation("购买装饰")
    public AjaxResult buy(@RequestBody BuyDTO dto){
        Long userId = SecurityUtils.getUserId();
        shopService.buy(userId, dto.getShopId(), dto.getDays());
        return AjaxResult.success();
    }


    @GetMapping("/getMyDecoration")
    @ApiOperation("根据类型获取我的装饰")
    public AjaxResult getMyDecoration(@RequestParam Integer type){
        Long userId = SecurityUtils.getUserId();
        List<Shop> userShop = shopService.getUserDecoration(userId, type,null);
        return AjaxResult.success(userShop);
    }

    @GetMapping("/getDecorationByType")
    @ApiOperation("根据类型获取我的装饰")
    public AjaxResult<List<Shop>> getDecorationByType(@RequestParam Integer category,
                                                      @RequestParam Integer type,
                                                      @RequestParam Integer page,
                                                      @RequestParam Integer size){
        Long userId = SecurityUtils.getUserId();
        List<Shop> shops = shopService.getDecorationByType(userId, category,type);
        return AjaxResult.success(shops);
    }


    @PostMapping("/useDecoration")
    @ApiOperation("使用装饰")
    public AjaxResult useDecoration(@RequestBody UseDecorationDTO dto){
        Long userId = SecurityUtils.getUserId();
        shopService.useDecoration(userId,dto.getShopId());
        return AjaxResult.success();
    }


    @PostMapping("/nonuseDecoration")
    @ApiOperation("不使用装饰")
    public AjaxResult nonuseDecoration(@RequestBody NonuseDecorationDTO dto){
        Long userId = SecurityUtils.getUserId();
        shopService.nonuseDecoration(userId, dto.getType());
        return AjaxResult.success();
    }



}
